<template>
  <el-dialog
    title="会员等级"
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    @close="dialogFormVisible"
  >
    <el-form size="small" :model="form">
      <el-form-item label="昵称" :label-width="formLabelWidth">
        <el-input v-model="form.nickName" autocomplete="off" disabled />
      </el-form-item>

      <el-form-item label="等级" :label-width="formLabelWidth">
        <el-select v-model="form.grade_id" placeholder="-请选择等级-">
          <el-option v-for="(item,index) in gradeList" :key="index" :label="item.name" :value="item.grade_id" />
        </el-select>
      </el-form-item>

      <el-form-item label="管理员备注" :label-width="formLabelWidth">
        <el-input v-model="form.remark" type="textarea" placeholder="请输入管理员备注" />
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible">取 消</el-button>
      <el-button type="primary" :loading="loading" @click="editUser">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import UserApi from '@/api/user.js'

export default {
  // eslint-disable-next-line vue/prop-name-casing,vue/require-prop-types
  props: ['open_edit', 'form', 'gradeList'],
  data() {
    return {
      /* 左边长度 */
      formLabelWidth: '120px',
      /* 是否显示 */
      loading: false,
      dialogVisible: false
    }
  },
  created() {
    this.dialogVisible = this.open_edit
  },
  methods: {
    /** 修改用户 **/
    editUser() {
      const self = this
      self.loading = true
      const params = self.form
      UserApi.edituser(params, true)
        .then(data => {
          self.loading = false
          if (data.code === 1) {
            self.$message({
              message: '恭喜你，用户修改成功',
              type: 'success'
            })
            self.dialogFormVisible(true)
          }
        })
        .catch(() => {
          self.loading = false
        })
    },
    /** 关闭弹窗 **/
    dialogFormVisible(e) {
      if (e) {
        this.$emit('closeDialog', {
          type: 'success',
          openDialog: false
        })
      } else {
        this.$emit('closeDialog', {
          type: 'error',
          openDialog: false
        })
      }
    }
  }
}
</script>
